import { InformationList } from '@/views/drag/package/components/Informations/index'
import { PackagesCategoryEnum, PackagesType, ConfigType,FetchComFlagType } from './index.d'
const indexModules: Record<string, { default: string }> = import.meta.glob('./components/**/index.vue', {
  eager: true
})
// * 所有图表
export let packagesList: PackagesType = {
  [PackagesCategoryEnum.INFORMATIONS]: InformationList,
}
// const fetchComponent = (chartName: string, flag: FetchComFlagType) => {
//   const module = flag === FetchComFlagType.VIEW ? indexModules : configModules
//   for (const key in module) {
//     const urlSplit = key.split('/')
//     if (urlSplit[urlSplit.length - 2] === chartName) {
//       return module[key]
//     }
//   }
// }
const configModules: Record<string, { default: string }> = import.meta.glob('./components/**/config.vue', {
  eager: true
})
/**
 * * 获取组件
 * @param {string} chartName 名称
 * @param {FetchComFlagType} flag 标识 0为展示组件, 1为配置组件
 */
const fetchComponent = (chartName: string, flag: FetchComFlagType) => {
  const module = flag === FetchComFlagType.VIEW ? indexModules : configModules
  for (const key in module) {
    const urlSplit = key.split('/')
    if (urlSplit[urlSplit.length - 2] === chartName) {
      return module[key]
    }
  }
}
/**
 * * 获取展示组件
 * @param {ConfigType} dropData 配置项
 */
export const fetchChartComponent = (dropData: ConfigType) => {
  const { key } = dropData
  console.log(fetchComponent(key, FetchComFlagType.VIEW)?.default)
  return fetchComponent(key, FetchComFlagType.VIEW)?.default
}

/**
 * * 获取配置组件
 * @param {ConfigType} dropData 配置项
 */
export const fetchConfigComponent = (dropData: ConfigType) => {
  const { key } = dropData
  return fetchComponent(key, FetchComFlagType.CONFIG)?.default
}
/**
 * * 获取目标组件配置信息
 * @param targetData
 */
export const createComponent = async (targetData: ConfigType) => {
  const { redirectComponent, category, key } = targetData
  // redirectComponent 是给图片组件库和图标组件库使用的
  console.log(targetData);
  
  if (redirectComponent) {
    const [packageName, categoryName, keyName] = redirectComponent.split('/')
    const redirectChart = await import(`./components/${packageName}/${categoryName}/${keyName}/config.ts`)
    return new redirectChart.default()
  }
  const chart = await import(`./components/${targetData.package}/${category}/${key}/config.ts`)
  console.log(chart)
  return new chart.default()
}